﻿using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProductionManage.Models.DataModel;
using ProductionManage.Models.Products;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using wangyaxin.Wang.Dto;
using wangyaxin.Wyx.Dto;

namespace wangyaxin.Wyx
{
    [ApiExplorerSettings(GroupName = "王亚鑫")]
    public class VendorAppService: ApplicationService,IVendorAppService
    {
        private readonly IRepository<Vendor, int> _VendorRepositoryt;

        public VendorAppService(IRepository<Vendor, int> vendorRepository)
        {
            _VendorRepositoryt = vendorRepository;
        }


        /// <summary>
        /// 获取供应商列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task<string> SelectVendor(string? VendorCode, string? VendorName, string? VendorEnName)
        {
            var vendortolist= await _VendorRepositoryt.GetQueryableAsync();
            vendortolist= vendortolist.WhereIf(!string.IsNullOrEmpty(VendorCode), m => m.VendorCode==VendorCode);
            vendortolist= vendortolist.WhereIf(!string.IsNullOrEmpty(VendorName), m => m.VendorName.Contains(VendorName));
            vendortolist=vendortolist.WhereIf(!string.IsNullOrEmpty(VendorEnName), m => m.VendorEnName.Contains(VendorEnName));
            var stert = vendortolist.MapToList<VendorDto>();
            return JsonConvert.SerializeObject(new PageDto<VendorDto> 
            { 
                code = "", 
                data = stert,
                msg = "", 
                count = stert.Count 
            }
            );
        }


        /// <summary>
        /// 添加供应商--王亚鑫
        /// </summary>
        /// <param name="input">产品数据传输对象</param>
        /// <returns></returns>

        public async Task AddVendor(CreateVendorDto input)
        {
            var product = ObjectMapper.Map<CreateVendorDto, Vendor>(input);
            await _VendorRepositoryt.InsertAsync(product);
        }

        /// <summary>
        /// 删除供应商--王亚鑫
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteVendor(int id)
        {
            await _VendorRepositoryt.DeleteAsync(id);
        }

        /// <summary>
        /// 反填供应商--王亚鑫
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Vendor> SelectById(int id)
        {
            var vendor = await _VendorRepositoryt.GetAsync(m=>m.Id==id);
            return vendor;
        }
        /// <summary>
        /// 修改供应商--王亚鑫
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<string> UpdateVendor(UpdateVendorDto input)
        {
            var vendor = await _VendorRepositoryt.GetAsync(input.Id);
            vendor.VendorCode = input.VendorCode;
            vendor.VendorName = input.VendorName;
            vendor.VendorEnName = input.VendorEnName;
            vendor.VendorDesc =input.VendorDesc;
            vendor.VendorLogo = input.VendorLogo;
            vendor.VendorLevel = input.VendorLevel;
            vendor.VendorScore = input.VendorScore;
            vendor.Address = input.Address;
            vendor.WebSite = input.WebSite;
            vendor.Email = input.Email;
            vendor.Tel = input.Tel;
            vendor.Contact = input.Contact;
            vendor.ContactTel = input.ContactTel;
            vendor.ContactEmail = input.ContactEmail;
            vendor.Address = input.Address;
            vendor.CreditCode = input.CreditCode;
            vendor.Remark = input.Remark;
            await _VendorRepositoryt.UpdateAsync(vendor);


            return JsonConvert.SerializeObject(new PageDto<UpdateVendorDto>
            {
                code = "200",
                data = null,
                msg = "更新成功",
                count = 0
            });
        }
           
        
      

    }
}
